﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using XNAGuiLib.Core.Events;

namespace XNAGuiLib.Core.ActionHandling
{
    public abstract class Action
    {

        #region Protected Field - InitialMouseInsideGic
        /// <summary>
        /// protected flag for the prerequisite of the mouse cursor inside the invoker-GIC's rectangle
        /// </summary>
        protected bool InitialMouseInsideGic;
        #endregion


        #region Action Public Field - Enabled
        // flag if the action is enabled or not (default: true)
        protected bool _enabled = true;
        /// <summary>
        /// gets or sets if this Action is Enabled
        /// </summary>
        public bool Enabled
        {
            get
            {
                return _enabled;
            }
            set
            {
                _enabled = value;
            }
        }
        #endregion
        
        #region Action Public Field - Command
        // command which should be executed when the action is invoked
        protected string _command = "";
        /// <summary>
        /// gets or sets  the Custom Command for this Action to execute on the target
        /// </summary>
        public string Command
        {
            get
            {
                return _command;
            }
            set
            {
                _command = value;
            }
        }
        #endregion

        #region Action Public Field - Target
        protected IAction _target;
        /// <summary>
        /// gets or sets the Actions IAction-target
        /// </summary>
        public IAction Target
        {
            get
            {
                return this._target;
            }
            set
            {
                this._target = value;
            }
        }
        #endregion

        #region Action Public Field - Invoker
        protected GIComponent _invoker;
        /// <summary>
        /// gets the GIComponent invoker
        /// </summary>
        public GIComponent Invoker
        {
            get
            {
                return this._invoker;
            }
        }
        #endregion


        #region Action Protected Field - KeyboardState
        // KeyboardState of current cycle
        protected KeyboardState _ks;
        /// <summary>
        /// gets the current KeyboardState used by this Action
        /// </summary>
        protected KeyboardState KeyboardState
        {
            get
            {
                return _ks;
            }
        }
        #endregion
        
        #region Action Protected Field - OldKeyboardState
        // old KeyboardState (from last update cycle)
        protected KeyboardState _oks;
        /// <summary>
        /// gets the old KeyboardState used by this Action
        /// </summary>
        protected KeyboardState OldKeyboardState
        {
            get
            {
                return _oks;
            }
        }
        #endregion

        #region Action Protected Field - MouseState
        // MouseState of current cycle
        protected MouseState _ms;
        /// <summary>
        /// gets the current MouseState used by this MouseAction
        /// </summary>
        protected MouseState MouseState
        {
            get
            {
                return _ms;
            }
        }
        #endregion

        #region Action Protected Field - OldMouseState
        // old MouseState (from last update cycle)
        protected MouseState _oms;
        /// <summary>
        /// gets the old MouseState used by this MouseAction
        /// </summary>
        protected MouseState OldMouseState
        {
            get
            {
                return _oms;
            }
        }
        #endregion



        public Action(GIComponent invoker, IAction target, string command)
        {
            this._invoker = invoker;
            this._target = target;
            this._command = command;
        }

        // check if the Action is initialized
        #region Action Protected Method - IsInitialized()
        protected bool IsInitialized()
        {
            if (_target != null && _invoker != null)
                return true;
            else return false;
        }
        #endregion


        #region Action Public Method - Free()
        public void Free()
        {
            this._invoker = null;
            this._target = null;
        }
        #endregion

        #region Action Public Method - ExecuteCheck(MouseState ms, KeyboardState ks)
        public void ExecuteCheck(MouseState ms, KeyboardState ks)
        {
            _ms = ms;
            _ks = ks;

            if (_invoker != null && _target != null && this._enabled)
            {
                if (MouseHandler.CheckCurrentOnePressedOldAllReleased(_ms, _oms)
                    && _invoker.Rectangle.Intersects(new Rectangle(_ms.X, _ms.Y, 1, 1)))
                    InitialMouseInsideGic = true;
                else if (MouseHandler.CheckCurrentAllReleasedOldOneReleased(_ms, _oms))
                    InitialMouseInsideGic = false;

                this.Check(ms, ks);
            }

            _oms = _ms;
            _oks = _ks;
        }
        #endregion



        #region Action Virtual Method - Check(MouseState ms, KeyboardState ks)
        protected virtual void Check(MouseState ms, KeyboardState ks)
        {

        }
        #endregion
    }
}
